from qturboframe.lib.webdriver.exceptions import SeleniumError
from qturboframe.lib.report.log import logger
from qturboframe.public_ui.h_ui.calendar import Calendar as HuiCalendar
from qturboframe.lib.utils.__datatime_format__ import deal_datetime

class Calendar(HuiCalendar):
    '''
    对操作员中心有的日历控件方法进行重写
    '''

    def set_value(self, value):
        """
        hui日历控件赋值

        :Args:
         - value: 日期值或者日期范围

        :Usage:
         - calendar.set_value('20200420')
         - calendar.set_value('20200420 - 20200520')
        """
        # 数据驱动选项
        text_list = value.split(' - ')
        if len(text_list) == 1:
            self.set_value_for_single(text_list[0])
        elif len(text_list) == 2:
            self.set_value_for_range(text_list[0], text_list[1])
        else:
            raise AttributeError('日期参数格式不规范！')

    def set_value_for_single(self, value):
        """
        操作员中心日历组件赋值
        重写说明：操作员中心calendar_div的元素获取方法有所不同

        :Args:
         - value: 日期值

        :Usage:
         - calendar.set_value('20200420')
         - calendar.set_value('2020/04/20')
         - calendar.set_value('2020-04-20')
        """
        re_displaynone = 'display: none;'
        self._element.find_element_by_attribute('tag name', 'input', timeout=self.timeout).secure_click()
        self.calendar_div = self._element.exist(
            'css selector', 'div.h-select-dropdown', timeout=0.1)
        if not self.calendar_div:
            self.calendar_div = self._driver.find_element_by_attribute('xpath', '//body/div[contains(@class, "h-date-picker-transfer") and not(contains(@style, "display: none;"))]', timeout=self.timeout)
        text = deal_datetime(value)
        year, month, day = text.split('/')
        # 选择year
        div_style = self.calendar_div.get_attribute('style')
        if re_displaynone not in div_style:
            self.__set_year(year)
        # 选择month
        div_style = self.calendar_div.get_attribute('style')
        if re_displaynone not in div_style:
            self.__set_month(month)
        # 选择day
        div_style = self.calendar_div.get_attribute('style')
        if re_displaynone not in div_style:
            self.__set_day(day)